import { CurrencyPipe } from '@angular/common';
import { Component, OnInit, ViewChild } from '@angular/core';
import { Router } from '@angular/router';
import { STComponent, STColumn, STChange, STRequestOptions, STData } from '@delon/abc/st';
import { SFComponent, SFSchema, SFDateWidgetSchema } from '@delon/form';
import { SearchDrawerService, ShipperBaseService } from '@shared';
import { NzModalService } from 'ng-zorro-antd/modal';
import { BasicTableComponent } from 'src/app/routes/commom';
import { AccountDetailComponent } from 'src/app/shared/components/account-detail/account-detail.component';
import { FreightAccountService } from '../../services/freight-account.service';

@Component({
  selector: 'app-driver-account',
  templateUrl: './driver-account.component.html',
  styleUrls: ['../../../commom/less/commom-table.less']
})
export class DriverAccountComponent extends BasicTableComponent implements OnInit {
  loading: boolean = true;
  schema: SFSchema = this.initSF();
  columns: STColumn[] = this.initST();

  constructor(
    public service: FreightAccountService,
    private router: Router,
    private modal: NzModalService,
    public searchDrawerService: SearchDrawerService
  ) {
    super(searchDrawerService);
  }

  ngOnInit(): void {}

  search() {
    this.st?.load(1);
  }
  beforeReq = (requestOptions: STRequestOptions) => {
    Object.assign(requestOptions.body, { accountType: 2 });
    if (this.sf) {
      Object.assign(requestOptions.body, { ...this.sf?.value });
      if (this.sf?.value.createTime) {
        Object.assign(requestOptions.body, {
          createTime: {
            start: this.sf?.value.createTime?.[0] || '',
            end: this.sf?.value.createTime?.[1] || ''
          }
        });
      }
    }
    this.loading = true;
    return requestOptions;
  };
  afterRes = (data: any[], rawData?: any) => {
    this.loading = false;
    return data;
  };
  showAccountDetail(item: any) {
    this.modal.create({
      nzTitle: '账户明细',
      nzContent: AccountDetailComponent,
      nzNoAnimation: true,
      nzWidth: 600,
      nzComponentParams: {
        params: { accountType: 2, roleId: item.roleId },
        url: '/api/fcc/accountBalance/getDriverAccountBalanceDetailByOperator'
      },
      nzFooter: null
    });
  }

  exportList() {
    this.service.exportStart({ ...this.sf?.value, pageSize: -1 }, this.service.$api_export_driver_account_page);
  }

  private initSF(): SFSchema {
    return {
      properties: {
        expand: {
          type: 'boolean',
          ui: {
            hidden: true
          }
        },
        userName: {
          type: 'string',
          title: '司机姓名',
          ui: { placeholder: '请输入' }
        },
        certificateNumber: {
          type: 'string',
          title: '证件号码',
          ui: { placeholder: '请输入' }
        },
        phone: {
          type: 'string',
          title: '手机号',
          ui: {
            placeholder: '请输入'
          }
        },
        ltdId: {
          type: 'string',
          title: '网络货运人',
          ui: {
            widget: 'select',
            placeholder: '请选择',
            allowClear: true,
            asyncData: () => this.service.getNetworkFreightForwarder()
          }
        },
        bankType: {
          type: 'string',
          title: '银行类型',
          enum: [
            { label: '全部', value: null },
            { label: '平安银行', value: '1' },
            { label: '浦发银行', value: '2' }
          ],
          ui: {
            widget: 'select',
            placeholder: '请选择'
          },
          default: null
        },
        virtualAccount: {
          type: 'string',
          title: '虚拟账户',
          ui: {
            placeholder: '请输入'
          }
        },
        createTime: {
          title: '创建时间',
          type: 'string',
          ui: {
            widget: 'sl-from-to-search',
            format: 'yyyy-MM-dd',
            placeholder: '请选择'
          } as SFDateWidgetSchema
        }
      }
    };
  }

  private initST(): STColumn[] {
    return [
      {
        title: '序号',
        className: 'text-center',
        width: '60px',
        format: (item: STData, col: STColumn, idx: number) => {
          return ((this.st.pi -1) * this.st.ps) + idx + 1 + ''
        } 
      },
      { title: '司机姓名', width: 140, index: 'name' },
      { title: '证件号码', width: 140, index: 'idNo' },
      { title: '手机号', width: 140, index: 'phone' },
      { title: '网络货运人', width: 180, index: 'ltdName' },
      { title: '银行类型', width: 120, index: 'bankTypeLabel' },
      { title: '虚拟账户', width: 140, index: 'virtualAccount' },
      {
        title: '可用余额',
        index: 'availableBalance',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.availableBalance }) }
      },
      {
        title: '冻结余额',
        index: 'freezeBalance',
        width: 140,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.freezeBalance }) }
      },
      {
        title: '累计提现金额',
        index: 'withdrawBalance',
        width: 160,
        type: 'widget',
        className: 'text-right',
        widget: { type: 'currency-chy', params: ({ record }) => ({ value: record.withdrawBalance }) }
      },
      { title: '账户总余额', render: 'availableBalance', className: 'text-right', width: 180 },
      { title: '创建时间', index: 'createTime', type: 'date', width: 150 },
      { title: '状态', index: 'stateDeletedLabel', width: 120 },
      {
        title: '操作',
        width: 120,
        className: 'text-center',
        fixed: 'right',
        buttons: [
          {
            text: '查看明细',
            acl: { ability: ['FINANCIAL-DRIVER-ACOUNT-view'] },
            click: item =>
              this.router.navigate(['/financial-management/driver-account/detail/' + item.id], {
                queryParams: {
                  name: item.name,
                  phone: item.phone,
                  ltdName: item.ltdName,
                  bankType: item.bankType,
                  projectId: item.projectId,
                  availableBalance: item.availableBalance,
                  enterpriseId: item.enterpriseId,
                  roleId: item.roleId,
                  ltdId: item.ltdId
                }
              })
          }
        ]
      }
    ];
  }

  parseFloat(num: any) {
    return parseFloat(num);
  }
}
